package com.jianglin.mapper;

import com.jianglin.model.Complaint;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@Component("complaintDao")
@SuppressWarnings("SpellCheckingInspection")
public interface ComplaintMapper{
     @Select("<script>" +
             "SELECT com.*, own.username, type.NAME " +
             "FROM complaint com " +
             "LEFT JOIN complaint_type type ON type.id = com.com_id " +
             "LEFT JOIN `owner` own ON own.id = com.owner_id " +
             "<where>" +
             "   <if test='comId != null and comId != \"\"'> " +
             "       AND com.com_id = #{comId} " +
             "   </if>" +
             "   <if test='status != null'> " +
             "       AND com.status = #{status} " +
             "   </if>" +
             "   <if test='ownerId != null'> " +
             "       AND com.owner_id = #{ownerId} " +
             "   </if>" +
             "</where>" +
             "</script>")
     @Results({
             @Result(property = "id", column = "id", id = true),
             @Result(property = "comId", column = "com_id"),
             @Result(property = "comDate", column = "com_date"),
             @Result(property = "handleDate", column = "handle_date"),
             @Result(property = "ownerId", column = "owner_id"),
             @Result(property = "status", column = "status"),
             @Result(property = "clr", column = "clr"),
             @Result(property = "remarks", column = "remarks"),
             @Result(property = "owner", column = "owner_id",
                     one = @One(select = "com.jianglin.mapper.OwnerMapper.findById", fetchType = FetchType.EAGER)),
             @Result(property = "type", column = "com_id",
                     one = @One(select = "com.jianglin.mapper.ComplaintTypeMapper.findById", fetchType = FetchType.EAGER))
     })
     List<Complaint> queryComplaintAll(Complaint complaint);

     @Insert("INSERT INTO complaint (com_id, com_date, handle_date, owner_id, status, clr, remarks) " +
             "VALUES (#{comId}, #{comDate}, #{handleDate}, #{ownerId}, #{status}, #{clr}, #{remarks})")
     @Options(useGeneratedKeys = true, keyProperty = "id")
     int insert(Complaint complaint);

     @Delete("DELETE FROM complaint WHERE id = #{id}")
     int deleteById(Long id);

     @Update("<script>" +
             "UPDATE complaint " +
             "<set>" +
             "   <if test='comId != null'>com_id = #{comId},</if>" +
             "   <if test='comDate != null'>com_date = #{comDate},</if>" +
             "   <if test='handleDate != null'>handle_date = #{handleDate},</if>" +
             "   <if test='ownerId != null'>owner_id = #{ownerId},</if>" +
             "   <if test='status != null'>status = #{status},</if>" +
             "   <if test='clr != null'>clr = #{clr},</if>" +
             "   <if test='remarks != null'>remarks = #{remarks},</if>" +
             "</set>" +
             "WHERE id = #{id}" +
             "</script>")
     int update(Complaint complaint);

     @Select("SELECT * FROM complaint WHERE id = #{id}")
     @Results({
             @Result(property = "id", column = "id", id = true),
             @Result(property = "comId", column = "com_id"),
             @Result(property = "comDate", column = "com_date"),
             @Result(property = "handleDate", column = "handle_date"),
             @Result(property = "ownerId", column = "owner_id"),
             @Result(property = "status", column = "status"),
             @Result(property = "clr", column = "clr"),
             @Result(property = "remarks", column = "remarks")
     })
     Complaint findById(Long id);

     @Select("SELECT * FROM complaint")
     List<Complaint> selectAll();
}
